<template>
    <a-modal :title="modal.title" :width="600" :confirmLoading="submitLoading" :visible="modal.visible" :footer="null" @cancel="close">
        <form-create v-model="modal.fApi" :rule="modal.rule" :option="modal.option" @btn-radio-change="radioChange"/>
        <div class="custom-drawer-footer">
            <a-button type="danger" v-if="!state" @click="applyTask">驳回</a-button>
            <a-button type="primary" v-if="state" @click="applyTask">确认发布</a-button>
        </div>
    </a-modal>
</template>
<script>
import { 
    allOnlineTaskParkApply,
    allOnlineTaskParkReject
} from '@/api/manage/task'
import {
    PARK_TASK_APPLY,
    OPTION_BLOCK,
} from './form'
export default {
    name: 'taskApply',
    data () {
        return {
            modal:{
                title:'任务审核',
                visible:false,
                fApi:{},
                rule:PARK_TASK_APPLY,
                option:OPTION_BLOCK,
                id:''
            },
            state:true,
            submitLoading:false
        }
    },
    methods:{
        add(item){
            this.modal.id = item.id;
            this.state = true;
            this.modal.visible = true;
            this.$nextTick(()=>{
                this.modal.fApi.resetFields();
            })
        },
        radioChange(val){
            this.state = val.target.value === 2 ? true:false;
        },
        applyTask(){
           this.modal.fApi.submit(async (formData, $f) => {
            try {
                if(formData.status === 1 && !formData.rejectReason){
                    return this.$message.error('驳回任务，请填写备注！');
                }
                let params = {};
                params.id = this.modal.id;
                params.rejectReason = formData.rejectReason;
                this.submitLoading = true;
                const {code} = formData.status === 2 ? await allOnlineTaskParkApply(params) : await allOnlineTaskParkReject(params);
                this.submitLoading = false;
                if(code === '1'){
                    this.$message.success('操作成功');
                    this.modal.visible = false;
                    this.$emit("on-ok");
                }
            } catch (error) {
                this.$message.info(JSON.stringify(error));
            }
        })
        },
        close(){
            this.modal.visible = false;
        }
    }
}
</script>
<style>
    .nums-len{
        float: right;
    line-height: 1.1;
    color: #999;
    }
</style>